package main

import "fmt"

func replace(mySlice []string) {
	mySlice[0] = "bobby"
}
func main() {
	//var identifier []type
	//var courses []string
	//var courses = []string{"django", "scrapy", "tornado"}
	//fmt.Printf("%T", courses)

	//courses := make([]string, 5)
	//fmt.Println(len(courses))

	//var courses = [5]string{"django", "scrapy", "tornado", "python", "asyncio"}
	//subCouse := courses[1:4]
	//replace(subCouse)
	//fmt.Println(subCouse)
	//fmt.Printf("%T", subCouse)

	//subCourse := *new([]int)
	//fmt.Println(subCourse)

	//fmt.Println(subCouse[1])
	//subCouse[1] = "bobby"
	//fmt.Println(subCouse)

	//subCourse2 := subCouse[1:3]
	//fmt.Printf("%T %v\n", subCourse2, subCourse2)
	//
	////	append 可以向切片追加元素
	//subCourse2 = append(subCourse2, "imooc")
	////fmt.Println(subCourse2)
	//
	//subCourse3 := make([]string, len(subCourse2))
	//copy(subCourse3, subCourse2)
	//fmt.Println(subCourse2)
	//fmt.Println(subCourse3)
	//subCourse4 := append(subCourse2, subCourse3...)
	//fmt.Println(subCourse4)
	//
	//deleteCourses := [5]string{"django", "scrapy", "tornado", "python", "asyncio"}
	//courseSlice := deleteCourses[:]
	//courseSlice = append(courseSlice[:1], courseSlice[2:]...)
	//fmt.Println(courseSlice)

	//切片的底层是数组
	//触发了扩容会新开辟一块内存   b指向以前的  c指向扩容后的

	a := make([]int, 0)
	b := []int{1, 2, 3}
	fmt.Println(copy(a, b))
	//特殊现象
	c := b[:]
	c[0] = 8
	fmt.Println(b)
	fmt.Println(c)
	//	特殊现象2
	c = append(c, 9)
	fmt.Println(b)
	fmt.Println(c)
	//	特殊3
	c[0] = 8
	fmt.Println(b)
	fmt.Println(c)
	//	特殊4
	fmt.Println(len(c))
	fmt.Println(cap(c)) //cap 容量
	//make初始化默认cap和len一样
	d := make([]int, 5, 6)
	fmt.Printf("len=%d, cap=%d\n", len(d), cap(d))

	slice2 := []int{1, 2, 3}
	fmt.Printf("len=%d, cap=%d\n", len(slice2), cap(slice2))
	/*
		扩容算法：
		首先判断，如果新申请容量(cap）大于2倍的旧容量（old.cap)，最终容量〈newcap）就是新申请的容量(cap)
		否则判断，如果旧切片的长度小于1024，则最终容量(newcap)就是旧容量(old.cap)的两倍，即(newcap=doublecap)
		否则判断，如果旧切片长度大于等于1024，则最终容量(newcap）从旧容量（old.cap）开始c环增加原来的1/4，郎(n如果最终容量(cap）计算值溢出，则最终容量(cap）就是新申请容量(cap)

	*/
	oldSlice := []int{1, 2, 3}
	newSlice := oldSlice
	newSlice[0] = 5
	fmt.Println(oldSlice)

	s1 := make([]int, 5, 6)
	s1 = append(s1, 6)
	fmt.Println(s1)

}
